#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "scene.h"
#include "ctrlboard.h"
#include "mg_Lcd_Driver.h"
#include "mg_Device.h"
#include "mg_Rs485Protocol.h"

/* widgets:
Layer_TiaoGuang
Background_TGD
Container_TGD_On
Container_TGD_Text
Text_TGD_Baifenhao
Text_TGD_Liangdu
Container_TGD_Button
PopupButton_TGD_OFF
PopupButton_TGD_Hui
PopupButton_TGD_Sun
Meter_TGD_Xuanzhuan
Icon_TGD_Pointer
CircleProgressBar_TGD_Light
Text_TGD_PercentNum
Icon_TGD_Jingdu
Container_TGD_Off
PopupButton_TGD_ON
Icon_TGD_JindutiaoOff
Button_Back_MainMenu
*/
//控件
static ITUBackground* Background_TGD;
static ITUMeter* Meter_TGD_Xuanzhuan;
static ITUContainer* Container_TGD_Off;
static ITUContainer* Container_TGD_On;
static ITUText* Text_TGD_Baifenhao;

bool TiaoGuangOnEnter(ITUWidget* widget, char* param)
{
	if (!Background_TGD)//首次加载
	{
		Background_TGD = ituSceneFindWidget(&theScene, "Background_TGD");
		assert(Background_TGD);
		Container_TGD_Off = ituSceneFindWidget(&theScene, "Container_TGD_Off");
		assert(Container_TGD_Off);
		Container_TGD_On = ituSceneFindWidget(&theScene, "Container_TGD_On");
		assert(Container_TGD_On);
		Meter_TGD_Xuanzhuan = ituSceneFindWidget(&theScene, "Meter_TGD_Xuanzhuan");
		assert(Meter_TGD_Xuanzhuan);
		Text_TGD_Baifenhao = ituSceneFindWidget(&theScene, "Text_TGD_Baifenhao");
		assert(Text_TGD_Baifenhao);
	}
	//更具参数初始化界面
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
	{
		ituWidgetSetVisible(Container_TGD_Off, false);
		ituWidgetSetVisible(Container_TGD_On, true);
		ituMeterSetValue(Meter_TGD_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0]);
	}
	else//关闭
	{
		ituWidgetSetVisible(Container_TGD_On, false);
		ituWidgetSetVisible(Container_TGD_Off, true);
		ituMeterSetValue(Meter_TGD_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0]);
	}
	//对“%”坐标判断
	if (Meter_TGD_Xuanzhuan->value >= 100)
		ituWidgetSetX(Text_TGD_Baifenhao, 141);
	else
		ituWidgetSetX(Text_TGD_Baifenhao, 123);
	Set_Rotate_Block(30);//旋转步进
	return true;
}

bool TiaoGuangOnLeave(ITUWidget* widget, char* param)
{
	return true;
}

bool TiaoGuangOnTimer(ITUWidget* widget, char* param)
{
	static int MeterValue = 0, MeterValue_L = 0;
	static uint32_t SendEndTime = Rotate_SendCmd_Time, SendTime = 0;

	//用户手势操作
	if (Action_Cmd.User_Action == Left_Slide || Action_Cmd.User_Action == Right_Slide)
	{
		GotoMainMenu();//返回主菜单
		Clear_User_Action();
	}

	//设备操作
	MeterValue = Meter_TGD_Xuanzhuan->value;
	if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开处理旋转动作
	{
		if (Action_Cmd.User_Action == Clockwise_Rotation && MeterValue < 100)//顺时针旋转
		{
			MeterValue += Action_Cmd.Action_Times;
			if (MeterValue > 100)MeterValue = 100;
			ituMeterSetValue(Meter_TGD_Xuanzhuan, MeterValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}
		else if (Action_Cmd.User_Action == Anticlockwise_Rotation && MeterValue > 0)//逆时针旋转
		{
			MeterValue -= Action_Cmd.Action_Times;
			if (MeterValue < 0)MeterValue = 0;
			ituMeterSetValue(Meter_TGD_Xuanzhuan, MeterValue);//设置Meter百分比值
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = OFF;//关闭10s自动获取状态
			Clear_User_Action();
		}
	}
	//结束操作设备状态执行
	if (MeterValue == MeterValue_L)
	{
		if (SendEndTime <= Rotate_SendCmd_Time)
			SendEndTime++;
		//停止操作0.6s后发送执行报文
		if (SendEndTime == Rotate_SendCmd_Time)
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
			MessageFlag[MSG_GET_DEVICE_STATE_EN] = ON;//允许10s自动获取状态
		}
	}
	else
	{
		SendEndTime = 0;
		TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = MeterValue;
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0x03;//命令调光
		TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0] = MeterValue;
	}
	//用MSG_GET_DEVICE_STATE_EN标记来表示当前是否在旋转动作中
	if (MessageFlag[MSG_GET_DEVICE_STATE_EN] == OFF)
	{
		if (SendTime < Rotating_SendCmd_Time)
			SendTime++;
		if (SendTime == Rotating_SendCmd_Time)//0.495s发送一次命令
		{
			SendTime = 0;
			RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
		}
	}
	//对“%”坐标判断
	if (Meter_TGD_Xuanzhuan->value >= 100)
		ituWidgetSetX(Text_TGD_Baifenhao, 141);
	else
		ituWidgetSetX(Text_TGD_Baifenhao, 123);
	//设备状态更新
	if (MessageFlag[MSG_GUI_STATE_UPDATE] == ON)
	{
		if (TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status == 1)//设备开
		{
			ituWidgetSetVisible(Container_TGD_Off, false);
			ituWidgetSetVisible(Container_TGD_On, true);
			ituMeterSetValue(Meter_TGD_Xuanzhuan, TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.data[0]);
		}
		else//关闭
		{
			ituWidgetSetVisible(Container_TGD_On, false);
			ituWidgetSetVisible(Container_TGD_Off, true);
		}
		MeterValue = Meter_TGD_Xuanzhuan->value;//防止设备循环上报
		MessageFlag[MSG_GUI_STATE_UPDATE] = OFF;
	}
	MeterValue_L = MeterValue;
	
	return true;
}
bool PopupButton_TGD_OFF_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 0;//状态关
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 1;//命令关
	printf("Moorgen INFO : Touch to turn off the AdjustLight\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

bool PopupButton_TGD_ON_Press(ITUWidget* widget, char* param)
{
	TFT_Device_State[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 1;//状态开
	TFT_Device_Cmd[DisplayValue.TFT_Display_Device].IT_Dev_STatus_Value.IT_Light_Status.status = 2;//命令开
	printf("Moorgen INFO : Touch to turn on the AdjustLight\n");
	RS485_Set_TFTDeviceState(DisplayValue.TFT_Display_Device);
    return true;
}

